package com.ayitubie.refuelweb.config.interceptor;

import com.ayitubie.refuelweb.config.interceptor.loginUserInterceptor.LoginUserArgumentResolver;
import com.ayitubie.refuelweb.config.interceptor.loginUserInterceptor.LoginUserInterceptor;
import com.ayitubie.refuelweb.config.interceptor.tokenInterceptor.TokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    TokenInterceptor tokenInterceptor;

    @Autowired
    LoginUserInterceptor loginUserInterceptor;

    @Autowired
    LoginUserArgumentResolver loginUserArgumentResolver;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(tokenInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/user/login")
                .excludePathPatterns("/user/checkUserExist/**")
                .excludePathPatterns("/user/register")
                .excludePathPatterns("/user/setCookie")
                .excludePathPatterns("/api/pushMessage")
                .excludePathPatterns("/api/test/pushToWeb")
                .excludePathPatterns("/test/webError")
                .excludePathPatterns("/minio/getFile");
        registry.addInterceptor(loginUserInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/user/login")
                .excludePathPatterns("/user/checkUserExist/**")
                .excludePathPatterns("/user/register")
                .excludePathPatterns("/user/setCookie")
                .excludePathPatterns("/api/pushMessage")
                .excludePathPatterns("/api/test/pushToWeb")
                .excludePathPatterns("/test/webError")
                .excludePathPatterns("/minio/getFile");
    }

//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**")
//                .allowedOrigins("*").exposedHeaders("*")
////                .allowedOriginPatterns("*")
//                .allowedMethods("GET", "POST", "PUT", "OPTIONS", "DELETE", "PATCH")
//                .allowCredentials(true)
//                .maxAge(3600);
//
//    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(loginUserArgumentResolver);
    }
}
